package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.geom.*;
	public class PixelateAction extends AbstractAction
	{
		private var fade:Number;
		private var pixelSize:Number;
		
		public function setValue(fade:Number, pixelSize:Number):PixelateAction
		{
			this.fade = fade;
			this.pixelSize = pixelSize;
			return this;
		}
		
		public override function getDescription():String
		{
			return "Mosaic (" + this.pixelSize.toFixed(0) +  ", " + this.fade.toFixed(0) + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			
			var sMatrix:Matrix = new Matrix();
			var lMatrix:Matrix = new Matrix();
			var scaledWidth:Number = old.width / this.pixelSize;
			var scaledHeight:Number = old.height / this.pixelSize;
			
			sMatrix.scale(1 / this.pixelSize, 1 / this.pixelSize);
			lMatrix.scale(pixelSize, pixelSize);
			
			var temp:BitmapData = new BitmapData(scaledWidth, scaledHeight, true, 0);
			temp.draw(old, sMatrix);
			
			var ret:BitmapData = new BitmapData(old.width, old.height, true, 0);
			ret.draw(temp, lMatrix);
			
			ret.merge(old, ret.rect, new Point(0,0), 256 - fade,
				256 - fade, 256 - fade, 256 - fade);
			return ret;
		}	
	}
}